package de.pcsoft.framework.jclr;

import de.pcsoft.framework.jclr.exceptions.JCLRException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Christoph
 * Date: 14.06.12
 * Time: 21:36
 * To change this template use File | Settings | File Templates.
 */
public final class JCLRResult {

    private JCLRError error;
    private final List<String> argumentsNotNeeded;
    private final Map<String, ? extends JCLRArgument> argumentMap;

    JCLRResult() {
        argumentsNotNeeded = new ArrayList<String>();
        argumentMap = new HashMap<String, JCLRArgument>();
    }

    /**
     * Returns TRUE if an error was thrown (while reading), otherwise FALSE
     *
     * @return
     */
    public boolean hasError() {
        return error != null;
    }

    /**
     * Returns the error or null, if no error is thrown (while reading)
     *
     * @return
     */
    public JCLRError getError() {
        return error;
    }

    void setError(JCLRError error) {
        this.error = error;
    }

    List<String> getArgumentsNotNeeded() {
        return argumentsNotNeeded;
    }

    Map<String, ? extends JCLRArgument> getArgumentMap() {
        return argumentMap;
    }

    /**
     * Returns the arguments they was not needed in the reading. There are the arguments too much
     *
     * @return
     */
    public String[] getNotNeededArguments() {
        return argumentsNotNeeded.toArray(new String[argumentsNotNeeded.size()]);
    }

    /**
     * Checks the argument was set
     *
     * @param key
     * @return
     */
    public boolean isArgumentSet(String key) {
        return argumentMap.containsKey(key);
    }

    /**
     * Get the argument defined by key (must be cast)
     *
     * @param key
     * @return
     */
    public JCLRArgument getArgument(String key) {
        return argumentMap.get(key);
    }

    /**
     * Find special argument defined by key (simple argument)
     *
     * @param key
     * @return
     * @throws JCLRException Is thrown if the argument type is wrong
     */
    public JCLRSimpleArgument getSimpleArgument(String key) throws JCLRException {
        if (!argumentMap.containsKey(key))
            return null;

        JCLRArgument argument = argumentMap.get(key);
        if (!(argument instanceof JCLRSimpleArgument))
            throw new JCLRException(
                    "Wrong type for simple argument, key: " + key + ", type: " + argument.getClass().getName());

        return (JCLRSimpleArgument)argument;
    }
}
